<?php

namespace App\Admin\Actions\ShopOrder;

use App\Library\Logic\OperateLogLogic;
use App\Models\ShopOrderModel;
use Encore\Admin\Actions\Response;
use Encore\Admin\Actions\RowAction;
use Encore\Admin\Facades\Admin;
use Illuminate\Http\Request;

class ShopOrderEditReceiverInfoAction extends RowAction
{
    public $name = '修改收货信息';

    public function handle(ShopOrderModel $model, Request $request): Response
    {
        if (is_null($model)) return $this->response()->error(__('errorCode.433'))->refresh();
        $model->order_contacts = $request->contacts;
        $model->order_contacts_mobile = $request->mobile;
        $model->order_contacts_address = $request->address;
        $model->save();
        OperateLogLogic::add(Admin::user()->user_id, 'admin.shopOrder.editReceiverInfo');
        return $this->response()->success('操作成功！')->refresh();
    }

    public function form(ShopOrderModel $model): void
    {
        $this->text('contacts', __('shop_order.orderContacts'))->required()->attribute('maxlength', 20)
            ->default($model->order_contacts);
        $this->text('mobile', __('shop_order.orderContactsMobile'))->required()->attribute('maxlength', 11)
            ->rules('regex:' . __('com.mobileReg'))->default($model->order_contacts_mobile);
        $this->textarea('address', __('shop_order.orderContactsAddress'))->required()
            ->attribute('maxlength', 255)->default($model->order_contacts_address);
    }
}
